<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>增加游戏</title>
    <link href="../common/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }

        .addgame {
            margin-top: 50px;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }

        h2 {
            color: #007bff;
            margin-bottom: 20px;
        }

        label {
            font-weight: bold;
        }

        textarea {
            resize: vertical;
        }

        button {
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <?php
    include './header.php';
    include_once '../conn.php';

    // 处理表单提交
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // 获取表单数据
        $title = $_POST["title"];
        $description = $_POST["description"];
        $publisher = $_POST["publisher"];
        $releaseDate = $_POST["release_date"];
        $rating = $_POST["rating"];
        $categoryId = $_POST["category"];

        // 处理游戏图片上传
        $targetDir = "../images/games/"; 
        $imageFileType = strtolower(pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION));
        $targetFile = $targetDir . uniqid() . '.' . $imageFileType;

        if (move_uploaded_file($_FILES["image"]["tmp_name"], $targetFile)) {
            // 修正文件路径为相对路径
            $relativePath = str_replace('..', '.', $targetFile);

            // 插入游戏数据
            $insertQuery = "INSERT INTO games (title, description, publisher, release_date, rating, category_id, image_path) 
                        VALUES ('$title', '$description', '$publisher', '$releaseDate', $rating, $categoryId, '$relativePath')";

            if ($conn->query($insertQuery) === TRUE) {
                echo "<div class='container'>";
                echo "<div class='alert alert-success' role='alert'>游戏添加成功</div>";
                echo "</div>";
            } else {
                echo "<div class='container'>";
                echo "<div class='alert alert-danger' role='alert'>Error: " . $insertQuery . "<br>" . $conn->error . "</div>";
                echo "</div>";
            }
        } else {
            echo "<div class='container'>";
            echo "<div class='alert alert-danger' role='alert'>上传图片失败</div>";
            echo "</div>";
        }

        $conn->close();
    }
    ?>
    <div class="addgame container">
        <h2 class="mb-3">增加游戏</h2>

        <!-- 游戏表单 -->
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
            <div class="mb-3">
                <label for="image" class="form-label">游戏图片</label>
                <input type="file" class="form-control" id="image" name="image" required>
            </div>
            <div class="mb-3">
                <label for="title" class="form-label">游戏名称</label>
                <input type="text" class="form-control" id="title" name="title" required>
            </div>
            <div class="mb-3">
                <label for="description" class="form-label">游戏介绍</label>
                <textarea class="form-control" id="description" name="description" rows="3" required></textarea>
            </div>
            <div class="mb-3">
                <label for="publisher" class="form-label">发行公司</label>
                <input type="text" class="form-control" id="publisher" name="publisher" required>
            </div>
            <div class="mb-3">
                <label for="release_date" class="form-label">发布时间</label>
                <input type="date" class="form-control" id="release_date" name="release_date" required>
            </div>
            <div class="mb-3">
                <label for="rating" class="form-label">评分</label>
                <input type="number" step="0.1" class="form-control" id="rating" name="rating" required>
            </div>
            <div class="mb-3">
                <label for="category" class="form-label">游戏类别</label>
                <select class="form-select" id="category" name="category" required>
                    <?php
                    // 查询类别列表
                    $queryCategories = "SELECT * FROM categories";
                    $resultCategories = $conn->query($queryCategories);

                    while ($rowCategory = $resultCategories->fetch_assoc()) {
                        echo "<option value='{$rowCategory['category_id']}'>{$rowCategory['name']}</option>";
                    }
                    ?>
                </select>
            </div>
            <button type="submit" class="btn btn-primary">提交</button>
        </form>
    </div>
</body>

</html>